package com.yungu.swift.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Zerrion
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils {
    private static Pattern UNDERSCORE_PATTERN = Pattern.compile("_(\\w)");
    private static final Pattern UPPER_CASE_LETTER = Pattern.compile("[A-Z]");

    private StringUtils() {

    }

    public static String buildUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public static String underscore2Camel(String str, boolean isFirstLowerCase) {
        if (isEmpty(str)) {
            return "";
        }
        str = str.toLowerCase();
        Matcher matcher = UNDERSCORE_PATTERN.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        if (isFirstLowerCase) {
            return sb.toString();
        } else {
            return sb.substring(0, 1).toUpperCase() + sb.substring(1);
        }
    }

    public static String underscore2Camel(String str) {
        return underscore2Camel(str, true);
    }

    public static String camel2Underscore(String str) {
        if (isEmpty(str)) {
            return "";
        }
        Matcher matcher = UPPER_CASE_LETTER.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    public static String inputStream2String(InputStream inputStream) {
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            int i;
            while ((i = inputStream.read()) != -1) {
                byteArrayOutputStream.write(i);
            }
            return byteArrayOutputStream.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 首字母转大写
     *
     * @param str 需要转换的字符串
     * @return String 转换后的字符串
     */
    public static String firstCharToUpper(String str) {
        char[] ch = str.toCharArray();
        if (ch[0] >= 'a' && ch[0] <= 'z') {
            ch[0] = (char) (ch[0] - 32);
        }
        return new String(ch);
    }

    public static String firstCharToLower(String str) {
        char[] ch = str.toCharArray();
        if (ch[0] >= 'A' && ch[0] <= 'Z') {
            ch[0] = (char) (ch[0] + 32);
        }
        return new String(ch);
    }

    public static Boolean isNull(Object object) {
        return object == null;
    }

    public static Boolean isNotNull(Object object) {
        return !isNull(object);
    }

    /**
     * 字符串比较
     * @param strA
     * @param strB
     * @return
     */
    public static Boolean equals(String strA,String strB){
        if(StringUtils.isEmpty(strA)&&StringUtils.isEmpty(strB)){
            return true;
        }
        return (strA+"").equals(strB);
    }

    /**
     * 字符串为空时取默认值
     */
    public static String defaultIfNull(String curValue,String defaultValue){
        return isEmpty(curValue) ? defaultValue : curValue;
    };

    /**
     * Object转字符串
     */
    public static String nullToString(Object value){
        if(value==null){
            return "";
        }
        return value.toString();
    };
}
